import axios from 'axios';

export let instance = axios.create({
    baseURL: 'http://localhost:3030/admin',
    timeout: 10000,
    headers: {'token': window.localStorage.getItem('token')}
});


function responseFilter(resolve, reject) {
    return function ({data}) {
        if (data.code === 200) {
            resolve(data.data);
        } else {
            reject(data.data)
        }
    }
}

export const get = (url, params) => {
    return new Promise((resolve, reject) => {
        instance.get(url, {params}).then(responseFilter(resolve, reject)).catch(x => {
            reject(x)
        })
    })
};
export const post = (url, params) => {
    return new Promise((resolve, reject) => {
        instance.post(url, params).then(responseFilter(resolve, reject)).catch(x => {
            reject(x)
        })
    })
};
export const put = (url, params) => {
    return new Promise((resolve, reject) => {
        instance.put(url, params).then(responseFilter(resolve, reject)).catch(x => {
            reject(x)
        })
    })
};

export const del = (url, params) => {
    return new Promise((resolve, reject) => {
        instance.delete(url, params).then(responseFilter(resolve, reject)).catch(x => {
            reject(x)
        })
    })
};